
package com.braintreepayments.cardform.view;

import com.braintreepayments.cardform.ResourceTable;
import com.braintreepayments.cardform.utils.CardType;
import ohos.agp.components.AttrSet;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Display a set of icons for a list of supported card types.
 */
public class SupportedCardTypesView extends DirectionalLayout {
    private List<CardType> mSupportedCardTypes = new ArrayList<>();
    private Image mImage1;
    private Image mImage2;
    private Image mImage3;
    private Image mImage4;
    private Image mImage5;
    private Image mImage6;
    private Image mImage7;
    private Image mImage8;
    private Image mImage9;
    private Image mImage10;
    private CardType currCardType;

    public SupportedCardTypesView(Context context) {
        super(context);
        init();
    }

    public SupportedCardTypesView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    public SupportedCardTypesView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutScatter.getInstance(getContext()).parse(
                ResourceTable.Layout_bt_card_types, this, true);
        mImage1 = (Image) findComponentById(ResourceTable.Id_iv_icon1);
        mImage2 = (Image) findComponentById(ResourceTable.Id_iv_icon2);
        mImage3 = (Image) findComponentById(ResourceTable.Id_iv_icon3);
        mImage4 = (Image) findComponentById(ResourceTable.Id_iv_icon4);
        mImage5 = (Image) findComponentById(ResourceTable.Id_iv_icon5);
        mImage6 = (Image) findComponentById(ResourceTable.Id_iv_icon6);
        mImage7 = (Image) findComponentById(ResourceTable.Id_iv_icon7);
        mImage8 = (Image) findComponentById(ResourceTable.Id_iv_icon8);
        mImage9 = (Image) findComponentById(ResourceTable.Id_iv_icon9);
        mImage10 = (Image) findComponentById(ResourceTable.Id_iv_icon10);
    }

    /**
     * Sets the supported {@link CardType}s on the view to display the card icons.
     *
     * @param cardTypes The {@link CardType}s to display
     */
    public void setSupportedCardTypes(CardType... cardTypes) {
        if (cardTypes == null) {
            cardTypes = new CardType[]{};
        }
        mSupportedCardTypes.clear();
        mSupportedCardTypes.addAll(Arrays.asList(cardTypes));
        initCard();
        setCardAlpha(1f);
    }

    private void initCard() {
        for (int i = 0; i < mSupportedCardTypes.size(); i++) {
            if (i == 0) {
                mImage1.setImageAndDecodeBounds(mSupportedCardTypes.get(0).getFrontResource());
            }
            if (i == 1) {
                mImage2.setImageAndDecodeBounds(mSupportedCardTypes.get(1).getFrontResource());
            }
            if (i == 2) {
                mImage3.setImageAndDecodeBounds(mSupportedCardTypes.get(2).getFrontResource());
            }
            if (i == 3) {
                mImage4.setImageAndDecodeBounds(mSupportedCardTypes.get(3).getFrontResource());
            }
            if (i == 4) {
                mImage5.setImageAndDecodeBounds(mSupportedCardTypes.get(4).getFrontResource());
            }
            if (i == 5) {
                mImage6.setImageAndDecodeBounds(mSupportedCardTypes.get(5).getFrontResource());
            }
            if (i == 6) {
                mImage7.setImageAndDecodeBounds(mSupportedCardTypes.get(6).getFrontResource());
            }
            if (i == 7) {
                mImage8.setImageAndDecodeBounds(mSupportedCardTypes.get(7).getFrontResource());
            }
            if (i == 8) {
                mImage9.setImageAndDecodeBounds(mSupportedCardTypes.get(8).getFrontResource());
            }
            if (i == 9) {
                mImage10.setImageAndDecodeBounds(mSupportedCardTypes.get(9).getFrontResource());
            }
        }
    }

    private void setCardAlpha(float alpha) {
        mImage1.setAlpha(alpha);
        mImage2.setAlpha(alpha);
        mImage3.setAlpha(alpha);
        mImage4.setAlpha(alpha);
        mImage5.setAlpha(alpha);
        mImage6.setAlpha(alpha);
        mImage7.setAlpha(alpha);
        mImage8.setAlpha(alpha);
        mImage9.setAlpha(alpha);
        mImage10.setAlpha(alpha);
    }

    /**
     * Selects the intersection between the {@link CardType}s passed into
     * {@link #setSupportedCardTypes(CardType...)} and {@link CardType}s passed into
     * this method as visually enabled.
     * <p>
     * The remaining supported card types will become visually disabled.
     * <p>
     * {@link #setSupportedCardTypes(CardType...)} must be called prior to using this method.
     *
     * @param cardTypes The {@link CardType}s to set as visually enabled.
     */
    public void setSelected(CardType cardTypes) {
        if (currCardType == cardTypes) {
            return;
        }
        if (currCardType == CardType.EMPTY) {
            initCard();
            return;
        }
        setCardAlpha(0.1f);
        currCardType = cardTypes;
        if (cardTypes == CardType.VISA) {
            mImage1.setAlpha(1f);
        } else if (cardTypes == CardType.MASTERCARD) {
            mImage2.setAlpha(1f);
        } else if (cardTypes == CardType.DISCOVER) {
            mImage3.setAlpha(1f);
        } else if (cardTypes == CardType.AMEX) {
            mImage4.setAlpha(1f);
        } else if (cardTypes == CardType.DINERS_CLUB) {
            mImage5.setAlpha(1f);
        } else if (cardTypes == CardType.JCB) {
            mImage6.setAlpha(1f);
        } else if (cardTypes == CardType.MAESTRO) {
            mImage7.setAlpha(1f);
        } else if (cardTypes == CardType.UNIONPAY) {
            mImage8.setAlpha(1f);
        } else if (cardTypes == CardType.HIPER) {
            mImage9.setAlpha(1f);
        } else if (cardTypes == CardType.HIPERCARD) {
            mImage10.setAlpha(1f);
        }
    }
}